package com.vdong.data.repository;

import javax.transaction.Transactional;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import com.vdong.data.model.Template;
import com.vdong.data.model.enums.State;

public interface TemplateRepository extends JpaRepository<Template, String> {

    Page<Template> findByUserIdAndUsersKeyAndStateLessThanOrderByCreateTimeDesc(String userId, String key, State state,
                                                                                Pageable pagebale);

    Template findByIdAndUsersKey(String id, String usersKey);

    Template findByUserIdAndUsersKeyAndState(String userId, String key, State state);

    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "update template p set p.state =?1 where p.user_id = ?2 and p.users_key=?3 and p.state=?4", nativeQuery = true)
    int updateStatusByUserIdAndState(int state, String userId, String key, int stateType);

}
